﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using System.Diagnostics;
#endregion

using GOAP.Entities;

namespace GOAP
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D testTexture;
        World world;

        public Game1()
            : base()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            //Init
            world = new World();

            //Create nodes
            Node node0 = world.createEntity<Node>("node");
            Node node1 = world.createEntity<Node>("node");
            Node node2 = world.createEntity<Node>("node");
            Node node3 = world.createEntity<Node>("node");
            Dwarf dwarf = world.createEntity<Dwarf>("dwarf");

            //Init node data
            node0.addConnection(node1);
            node1.addConnection(node2);
            node2.addConnection(node3);

            dwarf.spawn(node1);

            //Init GOAP data

            //Add nodes to world
            world.addEntity(node0);
            world.addEntity(node1);
            world.addEntity(node2);
            world.addEntity(node3);
            world.addEntity(dwarf);

            world.printEntities();

            //TEST RUN
            Stopwatch sw = new Stopwatch();
            System.Console.WriteLine("Test run start");
            sw.Start();

            World currentWorld = world;

            Random rand = new Random();
            int runs = 100000;
            for (int i = 0; i < runs; i++)
            {
                Dwarf d = currentWorld.getEntity<Dwarf>(dwarf.name);
                List<Action> actions = d.getPossibleActions();

                Action act = actions[rand.Next(0, actions.Count)];
                //act.print();

                World copy = currentWorld.clone();
                act.goapAct(copy, d);

                //next itteration
                currentWorld = copy;
            }
            sw.Stop();
            System.Console.WriteLine("Test run end");
            System.Console.WriteLine("Finished " + runs + " runs in " + sw.ElapsedMilliseconds + " ms");


            //Dont touch!
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            testTexture = Content.Load<Texture2D>("test");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(testTexture, new Rectangle(0,0, 16, 16), Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
